<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Schedule extends CI_Controller {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('schedule_model');
        $this->load->model('movie_model');
        $this->load->model('cinema_model');
        $this->load->model('branch_model');
        $template['current_class'] = "schedule";
    }

	public function index()
	{
	    $data['schedules'] = $this->schedule_model->get_all(20, $this->uri->segment(4));
	    $template['content'] = $this->load->view('admin/schedule', $data, TRUE);
		$this->load->view('admin/template', $template);
	}
	
	public function create()
	{
	    $this->form_validation->set_rules('cinema_id','movie_id','start_time','end_time','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $schedule_editor['title'] = "Create Schedule";
	    $schedule_editor['form_url'] = site_url('admin/schedule/create');
	    $schedule_editor['cinemas'] = $this->cinema_model->get_all();
	    $schedule_editor['movies'] = $this->movie_model->get_all();
	    if($this->form_validation->run())
    	{
    	    $cinema_id = $this->input->post('cinema_id', TRUE);
            $movie_id = $this->input->post('movie_id', TRUE);
            $start_time = $this->input->post('start_time', TRUE);
            $end_time = $this->input->post('end_time', TRUE);
            $result = $this->schedule_model->create($cinema_id, $movie_id, $start_time , $end_time);
            if ($result) 
            {
                redirect('admin/schedule');
            }else{
                $schedule_editor['errors'] = 'มีการใช้โรงหนังนี้ในช่วงเวลานี้ไปแล้ว.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/schedule_editor', $schedule_editor, TRUE);
		$this->load->view('admin/template', $template);
    	
	}
	
	
	
	function edit($schedule_id)
    {
        $this->form_validation->set_rules('cinema_id','movie_id','start_time','end_time','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $schedule_editor['title'] = "Edit Schedule";
	    $schedule_editor['form_url'] = site_url('admin/schedule/edit/'.$schedule_id);
	    $schedule_editor['cinemas'] = $this->cinema_model->get_all();
	    $schedule_editor['movies'] = $this->movie_model->get_all();
	    $schedule_editor['schedule'] = $this->schedule_model->get_by_id($schedule_id);
	    if($this->form_validation->run())
    	{
    	    $cinema_id = $this->input->post('cinema_id', TRUE);
            $movie_id = $this->input->post('movie_id', TRUE);
            $start_time = $this->input->post('start_time', TRUE);
            $end_time = $this->input->post('end_time', TRUE);
            $result = $this->schedule_model->update($schedule_id,$cinema_id, $movie_id, $start_time , $end_time);
            if ($result) 
            {
                redirect('admin/schedule');
            }else{
                $schedule_editor['errors'] = 'มีการใช้โรงหนังนี้ในช่วงเวลานี้ไปแล้ว.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/schedule_editor', $schedule_editor, TRUE);
		$this->load->view('admin/template', $template);
    }
    
    function destroy($schedule_id)
    {
        $this->schedule_model->delete($schedule_id);
        redirect('admin/schedule');
    }
}